import java.sql.*;

public class StuManager  {
    private static Connection connection;

    //构造方法
    public StuManager() {
    }
    public StuManager(String url,String name,String password) throws SQLException {
        //创建数据库连接
        connection = DriverManager.getConnection(url,name,password);
    }

    //关闭连接
    public static void close() throws SQLException {
        connection.close();
    }



    //增加  学生信息
    public void  addStu(Stu stu) throws SQLException {
        //定义一个输入语句
        String query ="INSERT INTO stu_table(id,name,score,phone)value(?,?,?,?)";
        //创建Prepared对象   数据库操作对象
        PreparedStatement preparedStatement = connection.prepareStatement(query);
        //设置参数
        preparedStatement.setInt(1,stu.getId());
        preparedStatement.setString(2,stu.getName());
        preparedStatement.setString(3,stu.getScore());
        preparedStatement.setString(4,stu.getPhone());
        System.out.println("******增加完毕！*****");

        //执行sql
        preparedStatement.executeUpdate();//update  对数据修改
        //关闭
        preparedStatement.close();
    }

    //查询
    public void getStu(int id) throws SQLException {
        //定义一个输入语句
        String query ="select * from stu_table " +
                "where id = ?";
        //创建Prepared对象   数据库操作对象
        PreparedStatement preparedStatement = connection.prepareStatement(query);
        //设置参数
        preparedStatement.setInt(1,id);

        //执行sql
        ResultSet resultset = preparedStatement.executeQuery();

        if(resultset.next()) {
            //从结果中提取数据
            String name = resultset.getNString("name");
            String score = resultset.getNString("score");
            String phone = resultset.getNString("phone");

            System.out.println("id：" +id);
            System.out.println("姓名：" +name);
            System.out.println("成绩：" +score );
            System.out.println("联系方式：" + phone );

            resultset.close();
            preparedStatement.close();

        }
        else {
            System.out.println("未查询到数据！！！");
            resultset.close();
            preparedStatement.close();

        }
    }

    //修改  学生信息
    public  void updateStu(int id,String score) throws SQLException {
        //定义一个输入语句
        String query ="UPDATE stu_table SET score =? " +
                "where id =?";
        //创建Prepared对象   数据库操作对象
        PreparedStatement preparedStatement = connection.prepareStatement(query);
        //设置参数
        preparedStatement.setInt(1,id);
        preparedStatement.setString(2,score);

        //执行sql
        preparedStatement.executeUpdate();//update  对数据修改
        //关闭
        preparedStatement.close();
    }

    //删除  学生信息
    public  void  deleteStu(int id) throws SQLException {
        //定义一个输入语句
        String query ="delete from stu_table where id=?";
        //创建Prepared对象   数据库操作对象
        PreparedStatement preparedStatement = connection.prepareStatement(query);
        //设置参数
        preparedStatement.setInt(1,id);
        System.out.println("******删除成功*****");
        //执行sql
        preparedStatement.executeUpdate();//update  对数据修改
        //关闭
        preparedStatement.close();

    }
}
